<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Driver_model
 *
 * @author peerawatj20
 */
class driver_model extends CI_Model {

    private $_table_driver = 'vpc_driver';
    private $_table_driver_image = 'vpc_driver_image';
    private $_table_prefix = 'vpc_catalog_prefix';
    private $_table_province = 'vpc_catalog_province';
    private $_table_status = 'vpc_catalog_status';

    public function save_data($prefix, $fname, $lname, $nname, $citizenId, $birthday, $address, $province, $postcode, $telephone, $status) {
        $arr_driver = array('prefix_id' => $prefix,
            'first_name' => $fname,
            'last_name' => $lname,
            'nick_name' => $nname,
            'citizen_id' => $citizenId,
            'birthday' => $birthday,
            'address' => $address,
            'province_id' => $province,
            'postcode' => $postcode,
            'telephone' => $telephone,
            'status_id' => $status,
            'is_deleted' => 'N',
            'created_by' => $this->session->userdata('username'),
            'created_date' => date("Y-m-d H:i:s")
        );

        $can_save = $this->db->insert($this->_table_driver, $arr_driver);
        return $can_save;
    }

    public function update_data($id, $prefix, $fname, $lname, $nname, $citizenId, $birthday, $address, $province, $postcode, $telephone, $status) {

        $arr_driver = array('prefix_id' => $prefix,
            'first_name' => $fname,
            'last_name' => $lname,
            'nick_name' => $nname,
            'citizen_id' => $citizenId,
            'birthday' => $birthday,
            'address' => $address,
            'province_id' => $province,
            'postcode' => $postcode,
            'telephone' => $telephone,
            'status_id' => $status,
            'updated_by' => $this->session->userdata('username'),
            'updated_date' => date("Y-m-d H:i:s")
        );

        $arr_where = array('id' => $id, 'is_deleted' => 'N');


        $can_update = $this->db->update($this->_table_driver, $arr_driver, $arr_where);
        return $can_update;
    }

    public function save_image($id, $name) {
        $arr_driver_image = array('driver_id' => $id,
            'file_name' => $name,
            'created_by' => $this->session->userdata('username'),
            'created_date' => date("Y-m-d H:i:s")
        );

        $this->db->insert($this->_table_driver_image, $arr_driver_image);
    }

    public function update_image($id, $name) {
        $arr_driver_image = array('file_name' => $name,
            'updated_by' => $this->session->userdata('username'),
            'updated_date' => date("Y-m-d H:i:s")
        );

        $arr_where = array('driver_id' => $id);

        $this->db->update($this->_table_driver_image, $arr_driver_image, $arr_where);
    }

    public function delete_driver_image($id) {
        $this->db->delete($this->_table_driver_image, array('driver_id' => $id));
    }

    public function delete_driver($id) {
        $arr_driver = array('is_deleted' => 'Y',
            'updated_by' => $this->session->userdata('username'),
            'updated_date' => date("Y-m-d H:i:s")
        );

        $arr_where = array('id' => $id);
        $this->db->update($this->_table_driver, $arr_driver, $arr_where);
    }

    public function get_driver_by_citizenId($citizenId) {
        $this->db->where('is_deleted', 'N');
        $query = $this->db->get_where($this->_table_driver, array('citizen_id' => $citizenId));
        return $query->result();
    }

    public function get_driver_by_id($id) {
        $this->db->select('*');
        $this->db->select($this->_table_driver . '.id as driver_id');
        $this->db->where($this->_table_driver . '.is_deleted', 'N');
        $this->db->join($this->_table_prefix, $this->_table_prefix . '.id = ' . $this->_table_driver . '.prefix_id', 'left');
        $this->db->join($this->_table_province, $this->_table_province . '.id = ' . $this->_table_driver . '.province_id', 'left');
        $this->db->join($this->_table_status, $this->_table_status . '.id = ' . $this->_table_driver . '.status_id', 'left');
        $query = $this->db->get_where($this->_table_driver, array($this->_table_driver . '.id' => $id));
        return $query->result();
    }

    public function get_driver_img_by_id($id) {
        $query = $this->db->get_where($this->_table_driver_image, array('driver_id' => $id));
        return $query->result();
    }

    public function count($fname = NULL, $lname = NULL, $nname = NULL, $status = NULL) {
        if (!is_null($fname) && !empty($fname)) {
            $this->db->like('first_name', $fname);
        }
        if (!is_null($lname) && !empty($lname)) {
            $this->db->like('last_name', $lname);
        }
        if (!is_null($nname) && !empty($nname)) {
            $this->db->like('nick_name', $nname);
        }
        if (!is_null($status) && !empty($status)) {
            $this->db->where('status_id', $status);
        }
        $this->db->where('is_deleted', 'N');

        $query = $this->db->get_where($this->_table_driver);
        return count($query->result());
    }

    public function search_list_driver($fname = NULL, $lname = NULL, $nname = NULL, $status = NULL, $limit = 10, $offset = 0) {
        $this->db->select('*');
        $this->db->select($this->_table_driver . '.id as driver_id');
        if (!is_null($fname) && !empty($fname)) {
            $this->db->like('first_name', $fname);
        }
        if (!is_null($lname) && !empty($lname)) {
            $this->db->like('last_name', $lname);
        }
        if (!is_null($nname) && !empty($nname)) {
            $this->db->like('nick_name', $nname);
        }
        if (!is_null($status) && !empty($status)) {
            $this->db->where('status_id', $status);
        }
        $this->db->where($this->_table_driver . '.is_deleted', 'N');
        $this->db->join($this->_table_prefix, $this->_table_driver . '.prefix_id = ' . $this->_table_prefix . '.id', 'left');
        $this->db->join($this->_table_status, $this->_table_status . '.id = ' . $this->_table_driver . '.status_id', 'left');
        $query = $this->db->get($this->_table_driver, $limit, $offset);
        return $query->result();
    }

}
